﻿using AloneLibrary.GameObjects;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using AloneLibrary.Services.Camera;

namespace Alone.GameObjects.Objects {
    class Tower : StaticGameObject {

        private Texture2D[] texs;
        private int i = 0;
       
        /// <summary>
        /// Initializes a new instance of the <see cref="Tower"/> class.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="position">The position.</param>
        /// <param name="direction">The direction.</param>
        /// <param name="scale">The scale.</param>
        public Tower(Model model, Effect effect, Vector3 position, Vector2 rotation, float scale)
            : base(model, effect, position, rotation, scale) {
            this.texs = new Texture2D[this.model.Meshes.Count];
            foreach (ModelMesh mesh in this.model.Meshes) {
                foreach (Effect e in mesh.Effects) {
                    this.texs[i] = effect.Parameters["Texture"].GetValueTexture2D();
                }
                i++;
            }
        }

        /// <summary>
        /// Draws the specified game time.
        /// </summary>
        /// <param name="gameTime">The game time.</param>
        /// <param name="camera">The camera.</param>
        public override void Draw(GameTime gameTime, ICameraHandler camera) {

            Matrix[] transforms = new Matrix[model.Bones.Count];
            model.CopyAbsoluteBoneTransformsTo(transforms);

            this.world = Matrix.CreateScale(this.Scale) *
                Matrix.CreateRotationY(this.Rotation.Y) *
                Matrix.CreateTranslation(this.Position);

            i = 0;
            foreach (ModelMesh mesh in this.model.Meshes) {
                foreach (ModelMeshPart part in mesh.MeshParts) {
                    part.Effect = this.effect;
                    effect.CurrentTechnique = effect.Techniques["Simplest"];
                    effect.Parameters["WorldViewProjection"].SetValue(this.world * camera.View * camera.Projection);
                    effect.Parameters["Texture"].SetValue(this.texs[i]);
                    effect.Parameters["World"].SetValue(this.world);
                    effect.Parameters["LightPos"].SetValue(new Vector3(-8, 4, -2));
                    effect.Parameters["LightPower"].SetValue(1.0f);
                    effect.Parameters["Ambient"].SetValue(0.2f);
                }
                i++;
                mesh.Draw();
            }
        }
    }
}
